<template>
  <div ref="page">
    <div class="head">
      <div>
        <div>创建人：{{ detailData.tenantName }}</div>
        <div>制单时间：{{ detailData.createTime }}</div>
        <div>单据编码：{{ detailData.checkCode }}</div>
      </div>
      <!-- <el-button type="info" text bg @click="$router.back()">返回</el-button> -->
    </div>

    <el-container class="content pb66">
      <el-aside>
        <!-- 商品列表 -->
        <div class="products-list" style="overflow-x: auto">
          <div class="my-table" style="min-width: 2100px">
            <div class="table-header">
              <div>
                <div>
                  <div class="col1">商品信息</div>
                  <div class="col3">关联客户</div>
                  <div class="col4">单位</div>
                  <div class="col7">仓库</div>
                  <div class="col5">账面库存数量</div>
                  <div class="col6">账面库存件数</div>
                  <div class="col5">实盘库存数量</div>
                  <div class="col6">实盘库存件数</div>
                  <div class="col5">盈亏数量</div>
                  <div class="col6">盈亏件数</div>
                  <div class="col5">账面采购金额</div>
                  <div class="col6">涉及采购金额</div>
                  <div class="remark">备注</div>
                </div>
              </div>
            </div>

            <div class="table-body" v-if="detailData?.inventoryCheckDetails?.length > 0">
              <div class="list-item" v-for="(item, index) in detailData.inventoryCheckDetails" :key="index">
                <p class="name">关联客户：{{ item.customer }}</p>
                <div>
                  <div class="col1">
                    <div>
                      <div class="left">
                        <img :src="item.mainPicUrl" alt="" />
                      </div>
                      <div class="right">
                        <h3 class="name text-break">{{ item.productAliasName || "-" }}</h3>
                        <p style="white-space: break-spaces; word-break: break-all">
                          <!-- {{ getSpes(item.properties) }} -->
                          {{ item.skuSpecification }}
                        </p>
                        <p>SKU编码：{{ item.skuCode || "-" }}</p>
                        <p>ID：{{ item.productId || "-" }}</p>
                      </div>
                    </div>
                  </div>
                  <div class="col3" style="white-space: break-spaces; word-break: break-all">
                    <!-- {{ item.customer ? item.customer.split(",")[0] + " " + item.customer.split(",")[1] : "-" }} -->
                    {{ item.customerName || "-" }}{{ item.customerMobileCode }}
                  </div>
                  <div class="col4">
                    {{ paramsOption.shop_unit.filter(v => v.dictValue == item.productUnit)[0].dictLabel }}
                  </div>
                  <div class="col7">{{ item.warehouseName || "-" }}</div>
                  <div class="col5">
                    {{ item.curProductNum || "-" }}
                  </div>
                  <div class="col6">
                    {{ item.curProductPartNum || "-" }}
                  </div>

                  <div class="col5">
                    {{ item.factProductNum || "-" }}
                  </div>
                  <div class="col6">
                    {{ item.factProductPartNum || "-" }}
                  </div>

                  <div class="col5">
                    {{ item.dispProductNum || "-" }}
                  </div>
                  <div class="col6">
                    {{ item.dispProductPartNum || "-" }}
                  </div>

                  <div class="col5">
                    {{ item.buyMoney || "-" }}
                  </div>
                  <div class="col6">
                    {{ item.dispBuyMoney || "-" }}
                  </div>
                  <div class="remark">
                    <RemarkEditor v-model="item.checkRemark" :isEdit="false" />
                  </div>
                </div>
                <!-- <div class="tips">
                  <p class="label">备注</p>
                  <RemarkEditor v-model="item.checkRemark" :isEdit="false" />
                </div> -->
              </div>
            </div>
            <div v-else style="display: flex; justify-content: center; width: 100%">
              <el-empty description="暂无数据" />
            </div>
          </div>
        </div>
      </el-aside>
      <el-main width="300px">
        <div class="file">
          <div>盘点标题</div>
          <span>{{ detailData.checkTitle }}</span>
          <div>盘点仓库</div>
          <span>{{ detailData.warehouseName }}</span>
          <div>盘点类型</div>
          <span>{{ detailData.checkType == 1 ? "整仓盘点" : "SKU盘点" }}</span>
          <el-divider></el-divider>
          <div>附件</div>
          <div class="list" v-if="detailData.picUrls?.length">
            <div class="code" v-for="file in detailData.picUrls" :key="file">
              <img :src="getIcon(file)" @click="download(file)" />
            </div>
          </div>
          <span v-if="!detailData.picUrls?.length">无</span>
          <template v-if="detailData.remark">
            <div>盘点备注</div>
            <RemarkEditor v-model="detailData.remark" :isEdit="false" />
          </template>
          <template v-else>
            <div>盘点备注</div>
            <RemarkEditor v-model="detailData.remark" :isEdit="false" />
          </template>
        </div>
      </el-main>
    </el-container>

    <!-- 操作按钮栏 -->
    <div class="action-wrap">
      <!-- 分页 -->
      <div>
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="pagination.sizes"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import word from "@/assets/images/word.png";
import pdf from "@/assets/images/pdf.png";
import video from "@/assets/images/video.png";
import excel from "@/assets/images/excel.png";
import { useRoute } from "vue-router";
import { useElementSize } from "@vueuse/core";
import { detailApi } from "@/apis/procure/inventory-count";
import { selectDictDataByType } from "@/apis/dict/dict";
import RemarkEditor from "@/components/RemarkEditor";

const page = ref(null);
const row = ref(null);
const tableRow = ref(null);
const { width } = useElementSize(page);
const route = useRoute();
const detailData = ref({
  fileUrls: []
});
const pagination = ref({
  page: 1,
  size: 10,
  sizes: [10, 20, 30, 40, 50],
  total: 0
});

//拼接商品的sku规格
const getSpes = properties => {
  if (!properties) return "-";
  const arr = JSON.parse(properties) || [];
  if (arr.length > 1) {
    const str = arr.reduce((a, b) => {
      return a.specificationVal + "+" + b.specificationVal;
    });
    return str;
  } else if (arr.length === 1) {
    return arr[0].specificationVal;
  } else {
    return "-";
  }
};

const getName = code => {
  return useCodeArrStore().getDictName("shop_unit", code);
};

const init = async () => {
  const res = await detailApi({ checkId: route.query.checkId, page: pagination.value.page, size: pagination.value.size });
  detailData.value = { ...res.data, picUrls: res.data.picUrls ? res.data.picUrls.split(",") : [] };
  pagination.value.total = res.data.total;
};

const handleSizeChange = val => {
  pagination.value.size = val;
  init();
};
const handleCurrentChange = val => {
  pagination.value.page = val;
  init();
};

const scroll = ({ scrollLeft }) => {
  row.value.scrollTo({
    left: scrollLeft
  });
  tableRow.value.scrollTo({
    left: scrollLeft
  });
};
/**
 * 根据类型返回相应图标
 */
const getIcon = url => {
  if (url) {
    let icon = "";
    if (getType(url) == "image") icon = url;
    else icon = icons[getType(url)];
    return icon;
  }
};

const fileTypes = {
  image: ["jpg", "png", "jpeg", "gif"],
  video: ["mp4", "avi"],
  excel: ["xls", "xlsx"],
  word: ["doc", "docx"],
  pdf: ["pdf"]
};
const icons = { word, pdf, excel, video };
/**
 * 获取文件类型
 */
const getType = url => {
  let type = "";
  for (let key in fileTypes) {
    fileTypes[key].filter(v => {
      if (url.indexOf(v) != -1) {
        type = key;
      }
    });
  }
  return type;
};
const paramsOption = ref({});
/**
 * 获取列表选项字典
 * @param {*} dictType 字典类型
 */
const getDict = async dictType => {
  const res = await selectDictDataByType(dictType);
  paramsOption.value[dictType] = res.data;
};
const download = url => {
  const link = document.createElement("a");
  link.href = url;
  link.click();
};
onMounted(() => {
  const dictArr = ["shop_unit"];
  dictArr.forEach(v => {
    getDict(v);
  });
  init();
});
</script>

<style lang="scss" scoped>
$border: 1px solid #dcdfe6;
$topic: #007dff;

.page {
  position: relative;
}

.action-wrap {
  position: absolute;
  bottom: 0;
  right: 0;
  left: 0;
  height: 64px;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0px -3px 10px 0px rgba(4, 24, 48, 0.1);
}

.head {
  display: flex;
  justify-content: space-between;
  border-bottom: $border;
  height: 62px;
  align-items: center;
  padding: 0 20px;

  > div:first-child {
    display: flex;
    gap: 30px;
  }

  .el-button {
    border-radius: 0;
  }
}

.content {
  height: calc(100vh - 197px);
  :deep(.el-aside) {
    flex: 1;
    padding: 20px;
    header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 18px;
      .supper {
        flex: 1;
        display: flex;
        align-items: center;
        padding-right: 20px;
        p {
          width: 65px;
        }
        .el-select {
          flex: 1;
        }
      }
    }
    .count {
      display: flex;
      justify-content: space-between;
      padding: 10px 20px;
      background-color: #f6f8fa;
      margin-top: 20px;
      margin-bottom: 20px;
      h3 {
        font-size: 16px;
        font-weight: normal;
      }
    }
    .products-list {
      .table-header {
        background-color: #f2f6fc;
        margin-bottom: 10px;
        & > div {
          align-items: center;
        }
      }
      .table-body {
        .list-item {
          margin-bottom: 10px;
          p.name {
            height: 40px;
            background: #f2f6fc;
            line-height: 40px;
            padding-left: 20px;
            border: 1px solid #e1e6ed;
            border-bottom: none;
          }
          & > div {
            border: 1px solid #e1e6ed;
            display: flex;
            & > div {
              border-right: 1px solid #e1e6ed;
              display: flex;
              align-items: center;
            }
            &:not(:first-of-type) {
              border-left: none;
            }
          }
          .col1 {
            & > div {
              display: flex;
            }

            .left {
              img {
                width: 80px;
                height: 80px;
                border-radius: 4px;
              }
            }
            .right {
              padding-left: 18px;
              .name {
                font-size: 14px;
                color: #3d3d3d;
                margin-bottom: 5px;
              }
              p {
                font-size: 12px;
                color: #86909c;
                margin-bottom: 5px;
              }
            }
          }
          .tips {
            display: flex;
            padding: 16px 14px;
            border-top: none;
            border-left: 1px solid #e1e6ed !important;
            p.label {
              width: 38px;
            }
            .el-input {
              flex: 1;
              .el-input__wrapper {
                background-color: #f2f3f5;
              }
            }
          }
        }
      }
      .table-header,
      .table-body {
        & > div > div:first-of-type {
          display: flex;
          justify-content: space-between;
          & > div {
            padding: 14px 20px;
            &.col1 {
              // width: 45%;
              flex: 1;
              min-width: 400px;
            }
            &.col4,
            &.col8 {
              width: 80px;
              box-sizing: border-box;
            }
            &.col3 {
              width: 80px;
            }
            &.col7 {
              width: 100px;
              box-sizing: border-box;
            }
            &.col5,
            &.col2,
            &.col6 {
              width: 125px;
              box-sizing: border-box;
            }
            &.remark {
              width: 224px;
              box-sizing: border-box;
            }
          }
        }
      }
    }
  }
  :deep(.el-main) {
    width: 300px;
    flex: none;
    background-color: #f6f8fa;
    border-left: 1px solid #e2e8f0;
    .pd-info,
    .ps-people {
      p {
        width: 120px;
        margin-bottom: 10px;
      }
      .el-select {
        flex: 1;
      }
    }
    .ps-people {
      & > div {
        display: flex;
        .el-button {
          font-size: 12px;
        }
      }
    }
    .kh-info {
      h3 {
        color: #86909c;
        font-weight: normal;
        margin: 20px 0 16px 0;
      }
      p {
        color: #86909c;
        margin-bottom: 10px;
      }
      b {
        font-weight: normal;
        color: #1a1a1a;
      }
    }
    .sh-info {
      & > p {
        font-size: 16px;
        color: #86909c;
        margin-bottom: 10px;
      }
      .el-form-item {
        flex-direction: column;
      }
      .el-form-item__label-wrap {
        margin-left: 0 !important;
      }
    }
    .ck-tips,
    .file {
      p.label {
        color: #4e5969;
        margin-bottom: 10px;
      }
    }

    .el-upload.el-upload--text {
      width: 80px;
      height: 80px;
      background-color: white;
      border-radius: 4px;
      .el-button {
        width: 100%;
        height: 100%;
      }
    }
    .el-upload-list__item.is-ready {
      width: auto;
      height: auto;
    }
  }
}

.list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: auto;
  gap: 10px;
  margin: 10px 0;

  .code {
    width: 80px;
    height: 80px;
    background: #f2f3f5;
    position: relative;

    &:hover {
      .del {
        display: flex;
      }
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .del {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(0, 0, 0, 0.2);
      display: none;
      color: #f2f3f5;
      font-size: 20px;
    }
  }

  .avatar-uploader {
    .upload {
      width: 80px;
      height: 80px;
      background: #f2f3f5;
      display: grid;
      grid-template-columns: 1fr;
      place-items: center;
      align-self: center;
      color: #c9cdd4;

      span {
        font-style: normal;
      }
    }

    .el-icon svg {
      font-size: 24px;
    }
  }
}

.tip {
  color: #86909c;
  font-size: 11px;
}

.order-remark {
  margin: 10px 0;
}

.el-scrollbar {
  height: unset !important;
  width: unset !important;
  border-left: $border;
  border-right: $border;
}

.el-scrollbar__wrap {
  height: unset !important;
  width: unset !important;
}

.remark {
  color: #1a1a1a;
}

.file {
  line-height: 26px;
  font-size: 12px;

  > div {
    color: #86909c;
  }

  > span {
    color: #1a1a1a;
    word-wrap: break-word;
  }
}
</style>
